package com.biz.primus.homepage.vo;

import com.biz.primus.homepage.enums.ElementsLoadType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newHashMap;

/**
 * Created by defei on 2/8/17.
 */
@Data
@ApiModel("首页数据")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CoverTemplateData extends CommonHtmlTemplateData implements Serializable {

    @ApiModelProperty("请求参数")
    private MethodExecutorMethodParam methodExecutorMethodParam;

    @ApiModelProperty("执行器")
    private String methodExecutor;

    @ApiModelProperty("加载数据策略")
    private ElementsLoadType elementsLoadType = ElementsLoadType.STATIC;

    @ApiModelProperty("开始时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date startTime;

    @ApiModelProperty("结束时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date endTime;

    @JsonIgnore
    transient private List<CoverTemplateData> dynamicElements;

    private List<CoverTemplateData> elements = newArrayList();

    private Map<String, CoverTemplateData> children = newHashMap();


    public CoverTemplateData getChild(String key){
        return children.get(key);
    }

    public void add(String key, CoverTemplateData coverTemplateData) {
        children.put(key, coverTemplateData);
    }

    @JsonProperty("elements")
    public List<CoverTemplateData> getElements(){
        return elements;
    }

    public CoverTemplateData getChild(List<String> dataSelectors) {
        if(CollectionUtils.isEmpty(dataSelectors)){
            return null;
        }
        try {
            CoverTemplateData child = this;
            for (String dataSelector : dataSelectors) {
                child = child.getChild(dataSelector);
            }
            return child;
        } catch (Exception e) {
            return null;
        }
    }

//    public CoverTemplateData getElement(List<String> dataSelectors, Integer index) {
//        CoverTemplateData child = getChild(dataSelectors);
//        if(child == null){
//            return null;
//        } else {
//            List<CoverTemplateData> elements = child.getElements();
//            return CollectionUtils.getCount(elements) > index ? elements.get(index) : null;
//        }
//    }

    @JsonIgnore
    public boolean atTheRightTime() {
        if (startTime == null || endTime == null) {
            return Boolean.TRUE;
        } else {
            long currentTimeMillis = System.currentTimeMillis();
            return startTime != null && endTime != null && currentTimeMillis >= startTime.getTime() && System.currentTimeMillis() <= endTime.getTime();
        }
    }
}
